<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<html>
<body>
<h2>Hello World!</h2>
<form action="/restful/test/" method="post">
    <input type="hidden" name="_method" value="DELETE">
    <input type="text" name="id" value="1">
    <input type="submit" value="DELETE请求">
</form>
<button onclick="send1()">ajax发送put請求</button>
<button onclick="send2()">ajax发送get請求</button>
<button onclick="send3()">ajax发送post請求</button>
<button onclick="send4()">ajax发送delete請求</button>
<button onclick="send5()">ajax post json数据</button>
</body>
<script type="text/javascript">
    function send1() {
        var xhr = new XMLHttpRequest();
        xhr.open("PUT", "/restful/test/1/"+encodeURI("张三")+"/"+encodeURI("男"));
        xhr.send();
    }

    function send2() {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "/restful/test/1");
        xhr.send();
    }

    function send3() {
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "/restful/test/1/"+encodeURI("李四")+"/"+encodeURI("女"));
        xhr.send();
    }

    function send4() {
        var xhr = new XMLHttpRequest();
        xhr.open("DELETE", "/restful/test/1");
        xhr.send();
    }
    function send5() {
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "/restful/test/user/json");
        //创建json数据
        var user={"name":"李世民","sex":"男","age":2000,"book":{"name":"《JAVA基础》","author":"xxx"},"hobbies":["羽毛球","乒乓球","爬山"]}
        var jsonStr = JSON.stringify(user);
        xhr.setRequestHeader('Content-type', 'application/json');
        xhr.send(jsonStr);
    }
</script>
</html>
